/***************************************************************************
    begin                : jan 01 2013
    copyright            : (C) 2013 by Benoit Valot
    email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class FragmentPage extends FragmentLemondeBase {
	private OnLinkSelectedListener listener;
	private String html;
	private WebView mywebview;

	public interface OnLinkSelectedListener {
		public void OnLinkSelected(Article art);
	}

	public static FragmentPage newInstance(String html) {
		FragmentPage fragment = new FragmentPage();
		Bundle bundle = new Bundle();
		bundle.putString("data", html);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			this.listener = (OnLinkSelectedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnLinkSelectedListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("lemonde", "onCreateView FragmentPage");
		View view = inflater.inflate(R.layout.page_view, container, false);
		this.html = this.getArguments().getString("data");
		mywebview = (WebView) view.findViewById(R.id.WebViewperso);
		this.load_content();
		return (view);
	}
	
	private void load_content() {
		final String mimeType = "text/html";
		final String encoding = "UTF-8";
		// on charge mon code html dans ma webview
		mywebview.loadDataWithBaseURL("http://mobile.lemonde.fr", this.html,
				mimeType, encoding, null);
		mywebview.setWebViewClient(new myWebViewClient());
		mywebview
				.setInitialScale((int) (this.get_datas().getZoomArticle() * mywebview
						.getScale()));
		mywebview.getSettings().setBuiltInZoomControls(this.get_datas().isZoomEnabled());
	}

	private class myWebViewClient extends WebViewClient {
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			try {
				Log.d("lemonde", "traitement url=" + url);
				if (url.matches(".*mp3.*")) {
					Log.d("lemonde", "Audio-" + url);
					Intent intent = new Intent();
					intent.setAction(android.content.Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.parse(url), "audio/*");
					startActivity(intent);
					return true;
				} else if (url.matches(".*mp4.*")) {
					Log.d("lemonde", "video-" + url);
					Intent intent = new Intent();
					intent.setAction(android.content.Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.parse(url), "video/*");
					startActivity(intent);
					return true;
				} else if (url.matches(".*lemonde.*")) {
					Article art = new Article();
					art.setUri(url);
					if (art.isMobileLink()) {
						Log.d("lemonde", "Chargement interne");
						FragmentPage.this.listener.OnLinkSelected(art);
					} else {
						Toast.makeText(
								FragmentPage.this.getActivity(),
								"Ce lien n'est pas visible sur l'application Android : Ouverture du navigateur",
								Toast.LENGTH_LONG).show();
						Intent i = new Intent(Intent.ACTION_VIEW);
						Uri u = Uri.parse(url);
						i.setData(u);
						startActivity(i);
					}
					return true;
				} else {
					Intent i = new Intent(Intent.ACTION_VIEW);
					Uri u = Uri.parse(url);
					i.setData(u);
					startActivity(i);
					return (true);
				}
			} catch (Exception e) {
				Log.e("lemonde", "Chargement du lien web");
				return false;
			}
		}
	};
}
